package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.CheckGroupService;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

@Autowired
    CheckGroupMapper checkGroupMapper;
    @Override
    public void addGroup(CheckGroup checkGroup, String checkitemIds) {
        checkGroupMapper.add(checkGroup);
        String[] split = checkitemIds.split(",");
        checkGroupMapper.addtoMidle(checkGroup.getId(),split);
    }

    @Override
    public void deleteById(Integer id) {
        checkGroupMapper.deleteMiddleById(id);
        checkGroupMapper.deleteGroupById(id);
    }

    @Override
    @Transactional
    public void edit(CheckGroup checkGroup, String checkitemIds) {
        String[] split = checkitemIds.split(",");
        checkGroupMapper.deleteMiddleById(checkGroup.getId());
        checkGroupMapper.addtoMidle(checkGroup.getId(),split);
        checkGroupMapper.edit(checkGroup);
    }

    @Override
    public Page<CheckGroup> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        String[] split = queryString.split(",");
        Map<String,String> map=new HashMap<>();
        for (String s : split) {
            String[] split1 = s.split("=");
            if(split1.length==2){
                map.put(split1[0],split1[1]);
            }
        }
        return checkGroupMapper.findPage(map);
    }
}
